<?php

# http://projects:8081/cdapp-2kt-api/api/seat_hours_declaration.php

include_once '_portal.php';
forbidden_acc();

$method = isset($post->method) ? $post->method : 'GET';
$startTime = isset($post->startTime) ? $post->startTime : (isset($_GET['beg']) ? $_GET['beg'] : '');
$endTime = isset($post->endTime) ? $post->endTime : (isset($_GET['end']) ? $_GET['end'] : '');
$filters = isset($post->filters) ? $post->filters : [];
$data = isset($post->data) ? $post->data : [];

if ($method == 'GET') {
  handle_get($startTime, $endTime, $filters);
} elseif ($method == 'SET') {
  handle_set($data);
}

function handle_get($startTime, $endTime, $filters) {
  create_table();
  $sql = "SELECT * FROM `2kt_seat_hours_declaration` WHERE `startTime` <= '{$endTime}' AND `endTime` >= '{$startTime}'";
  if (count($filters) > 0) {
    foreach ($filters as $entry) {
      if (is_array($entry->value)) {
        $sql .= " AND `{$entry->field}` IN ('" . implode("','", $entry->value) . "')";
      } else {
        $sql .= " AND `{$entry->field}` = '{$entry->value}'";
      }
    }
  }

  $result = exec_sql($sql);
  $result = $result ? $result : []; // 避免查询不到结果返回null或false，而应该返回空数组
  $count = count($result);
  output(['count' => $count, 'content' => $result, 'sql' => $sql]);
}

function handle_set($data) {
  create_table();
  $sql = [];
  foreach ($data as $cell) {
    $sql[] = "INSERT INTO `2kt_seat_hours_declaration`(`userName`, `userId`, `orgId`, `startTime`, `endTime`, `seatCode`, `seatRole`, `seatRoleId`, `seatPost`, `seatPostId`, `witnessName`, `witnessId`, `asHour`, `remark`) VALUES ('{$cell->userName}',{$cell->userId},{$cell->orgId},'{$cell->startTime}','{$cell->endTime}','{$cell->seatCode}','{$cell->seatRole}',{$cell->seatRoleId},'{$cell->seatPost}',{$cell->seatPostId},'{$cell->witnessName}',{$cell->witnessId},{$cell->asHour},'{$cell->remark}')";
  }
  $result = exec_sql_array($sql);

  $success = !in_array(false, $result);
  $failed_sql = [];
  if (!$success) {
    foreach ($result as $key => $row) {
      $failed_sql[] = $sql[$key];
    }
  }
  // 'details' => $success ? null : $result
  output(['success' => $success, 'failed' => $failed_sql]);
}

function create_table() {
  $sql = "CREATE TABLE IF NOT EXISTS `2kt_seat_hours_declaration` (
    `number` int(8) NOT NULL AUTO_INCREMENT,
    `userName` varchar(8) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
    `userId` int(16) NOT NULL,
    `orgId` int(4) NOT NULL,
    `startTime` datetime NOT NULL,
    `endTime` datetime NOT NULL,
    `seatCode` varchar(8) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
    `seatRole` varchar(8) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
    `seatRoleId` int(4) NOT NULL,
    `seatPost` varchar(8) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
    `seatPostId` int(4) NOT NULL,
    `witnessName` varchar(8) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
    `witnessId` int(4) NOT NULL,
    `asHour` int(2) NOT NULL DEFAULT '0',
    `remark` text COLLATE utf8_unicode_ci NOT NULL,
    `version` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`number`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  return exec_sql($sql);
}